JBoss Community Archive (Read Only)

SwitchYard 0.8

Auditing Exchanges

Since 0.6 release SwitchYard supports basic audit mechanism. The Auditing stuff require CDI environment to run, in other words META-INF/beans.xml is necessary. It's worth to note that auditing stuff works also in test environment.

Mediation states

SwitchYard exchange sent from service consumer to service provider goes trough few states. Depends on state you might have different message payload and different metadata on exchange:

  • Domain handlers - it's a place where all handlers defined in switchyard.xml are executed, it's early phase of mediation where you can implement own logic or chose service provider using own logic.

  • Addressing - if none from domain handlers specified providing service then addressing handler determines it using consumer contract.

  • Transaction - when service requires transaction this handler starts it

  • Security - verifies contraints related to authentication and authorization

  • General policy - executes other checks different than security and transaction

  • Validation - executes custom validators

  • Transformation - prepare payload for calling providing service

  • Validation - validates transformed payload

  • Provider call - calls providing service

  • Transaction - commits or rollbacks transaction, if necessary

If service consumer is synchronous and exchange pattern is set to in-out then some from these handlers are called once again:

  • Domain handlers - called with response generated by provider service

  • Validation - verifies output generated by provider

  • Transformation - converts payload to structure required by consumer

  • Validation - checks output after transformation

  • Consumer callback - returns exchange to service consumer

Writing custom auditors

As said before audit mechanism requires CDI runtime to work. Annotate your Auditor implementations with the @Named annotation in order to have Camel recognise them. Camel Exchange Bus (a default implementation used by SwitchYard) look up for bean definitions with @Audit annotation. Simplest auditor looks following:

@Audit
@Named("custom auditor")
public class SimpleAuditor implements Auditor {

    @Override
    public void beforeCall(Processors processor, Exchange exchange) {
        System.out.println("Before " + processor.name());
    }

    @Override
    public void afterCall(Processors processor, Exchange exchange) {
        System.out.println("After " + processor.name());
    }

}

You will see lots of statements printed in server console like 'Before DOMAIN_HANDLERS', 'Before ADDRESSING' and so on. Every step of mediation is surrounded by this SimpleAuditor class.

Be aware that afterCall method is not called if surrounded step thrown an exception. In this case call of afterCall is skipped.

Chosing exact places for audit

If you are not interested in all these states of SwitchYard mediation process you might provide argument to @Audit annotation. Accepted values must come from org.switchyard.bus.camel.processors.Processors enumeration. For example @Audit(Processors.VALIDATION) will handle only validation occurances.

Please remember that validation is executed twice for in-only exchanges and fourth times for in-out. Validation occurs before transformation of inbound message and after transformation. In case when outgoing message will be sent from SwitchYard service validation is executed before transformation of outbound message and after transformation.

Transformation is executed once for in-only exchanges and twice for in-out exchanges.

Transaction phase is always executed twice.

If you are interested in only one execution of your auditor use following combination @Audit(Processors.PROVIDER_CALLBACK). Auditor will be executed just before sending exchange to service implementation. It's also possible to stick one auditor instance with few mediation steps. For example bean with annotation @Audit({Processors.PROVIDER_CALLBACK, Processors.CONSUMER_CALLBACK}) will be executed twice. One pair of before/after call for provider service and second pair for outgoing response.

Important notes

Custom auditors should not preserve state inside any fields because dispatching order is not guaranteed and only one instance of auditor is created by default. If you would like store some values please use exchange properties or message headers. Example below shows how to count processing time using Exchange properties as temporary storage.

@Named("custom auditor")
public class SimpleAuditor implements Auditor {

    private Logger _logger = Logger.getLogger(SimpleAuditor.class);

    @Override
    public void beforeCall(Processors processor, Exchange exchange) {
        exchange.setProperty("time", System.currentTimeMillis());
    }

    @Override
    public void afterCall(Processors processor, Exchange exchange) {
        long time = System.currentTimeMillis() - exchange.getProperty("time", 0, Long.class);
        _logger.info("Step " + processor.name() + " took " + time + "ms");
    }

}
JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-13 09:51:30 UTC, last content change 2012-11-26 20:53:35 UTC.